package com.delong.service.market;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.delong.dao.DaoHelper;
import com.delong.dao.market.SourceBaseMapper;
import com.delong.model.market.SourceBase;

public class SourceBaseService {
	
	public void addElement(SourceBase element) {
		SqlSession session = null;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			SourceBaseMapper mapper = session.getMapper(SourceBaseMapper.class);
			mapper.addElement(element);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public List<SourceBase> getListElement(String title,int pageno, int limit) {
		SqlSession session = null;
		List<SourceBase> result = new ArrayList<SourceBase>();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			SourceBaseMapper mapper = session.getMapper(SourceBaseMapper.class);
			result = mapper.selectList(title, pageno, limit);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;

	}
	
	public List<SourceBase> getALLElement() {
		SqlSession session = null;
		List<SourceBase> result = new ArrayList<SourceBase>();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			SourceBaseMapper mapper = session.getMapper(SourceBaseMapper.class);
			result = mapper.selectALL();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;

	}

	public int getListElementCount(String title) {
		SqlSession session = null;
		int result = 0;
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			SourceBaseMapper mapper = session.getMapper(SourceBaseMapper.class);
			result = mapper.selectListcount(title);
			System.out.println(result);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return result;
	}

	public void changeStatus(int id, int status) {
		SqlSession session = null;

		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			SourceBaseMapper mapper = session.getMapper(SourceBaseMapper.class);
			mapper.changeStatus(id, status);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public void changeElement(SourceBase element) {
		SqlSession session = null;

		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			SourceBaseMapper mapper = session.getMapper(SourceBaseMapper.class);
			mapper.changeElement(element);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public SourceBase getElement(int id) {
		SqlSession session = null;
		SourceBase element = new SourceBase();
		try {
			session = DaoHelper.getSqlSessionFactory().openSession(true);
			SourceBaseMapper mapper = session.getMapper(SourceBaseMapper.class);
			element = mapper.selectElementById(id);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return element;

	}

}
